<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 开放平台管理模块
 */
class Platopen extends Admin_Controller {
	
    function __construct()
    {
        parent::__construct();
        $this->load->model('plat_model');
        $this->load->model('plat_cpa_model');
        $this->load->model('users_model');
        $this->load->model('plat_invest_model');
        $this->load->model('plat_award_list_model');
        $this->load->model('plat_account_model');
    }
    //平台账号列表
    public function index()
    {
        $where = array();
        $st = trim($_GET['st']);
        $key = trim($_GET['key']);
        if ($key != '') {
            if ($st == 1) {
                $con = '平台中文名称';
                $where['name_ch'] = $key;
            }
            if ($st == 2) {
                $con = '平台英文名称';
                $where['name_en'] = $key;
            }
        }
        if (trim($_GET['time-start']) && trim($_GET['time-end'])) {
            $time_start = strtotime($_GET['time-start']);
            $time_end = strtotime($_GET['time-end']) + 3600 * 24;
            $where['plat.create_time <= '] = $time_end;
            $where['plat.create_time >= '] = $time_start;
        }
        //分页
       $getcount = $this->plat_account_model->get_account_count($where);
       $navigation_position = 'outside';
       $records_per_page = 10;
       $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
       //$this->xpage->reverse(true);
       $this->xpage->records($getcount);
       $this->xpage->records_per_page($records_per_page);
       $this->xpage->labels("<<", ">>");
       $list = $this->plat_account_model->get_account_list($where, (($this->xpage->get_page() - 1) * $records_per_page) ,$records_per_page);
       foreach ($list as $key =>$val){
           $list[$key]['last_time'] = $val['last_time'] > 0 ? date("Y-m-d H:i:s", $val['last_time']) : "";
       }
       //------------------
       $this->template->assign('stname', $st);
        $this->template->assign('keyname', trim($_GET['key']));
        $this->template->assign('conname', $con);
        $this->template->assign('time_start', trim($_GET['time-start']));
        $this->template->assign('time_end', trim($_GET['time-end']));
       $this->template->assign('Total',$getcount);
       $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$list);
        $this->template->load('platopen/index');
    }
    
    //添加平台账户
    public function account_add(){
        if($_POST){
            $plat_en = isset($_POST['plat_en']) ? trim($_POST['plat_en']) : "";
            $username = isset($_POST['username']) ? trim($_POST['username']) : "";
            $password = isset($_POST['password']) ? trim($_POST['password']) : "";
            $plat_name = isset($_POST['plat_name']) ? trim($_POST['plat_name']) : "";
            $is_lock = trim($_POST['is_lock']);
            if(!$plat_en){ 
                $mssage['info']	  = '请选择平台账户所属';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            if(!$username){ 
                $mssage['info']	  = '用户名不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            if(!$password){ 
                $mssage['info']	  = '密码不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            $plat_logo = trim($_POST['plat_logo']);
            $contacter = trim($_POST['contacter']);
            $tell_mobile = trim($_POST['tell_mobile']);
            $tell_phone = trim($_POST['tell_phone']);
            $address = trim($_POST['address']);
            
            $plat = $this->plat_model->get(array('name_en'=>$plat_en));
            
            $plat_account = array();
            $plat_account['plat_name'] = $plat_name;
            $plat_account['plat_ch'] = $plat['name_ch'];
            $plat_account['plat_en'] = $plat['name_en'];
            $plat_account['username'] = $username;
            $plat_account['password'] = md5($password);
            $plat_account['plat_logo'] = $plat_logo;
            $plat_account['contacter'] = $contacter;
            $plat_account['tell_mobile'] = $tell_mobile;
            $plat_account['tell_phone'] = $tell_phone;
            $plat_account['address'] = $address;
            $plat_account['is_lock'] = $is_lock;
            $plat_account['create_time'] = time();
            $result = $this->plat_account_model->insert($plat_account);
            if($result){
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = "platopen/index";
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = "platopen/index";
                echo json_encode($mssage);exit;
            }
        }
        
        $plats = $this->plat_model->get_distinct_plat();
        $this->template->assign('plats',$plats);
        $this->template->assign('segment_p','index');
        $this->template->load('platopen/account_add');
    }
    
    //修改平台账户信息
    public function account_edit(){
        if($_POST){
            
            $id = isset($_POST['id']) ? trim($_POST['id']) : "";
            $username = isset($_POST['username']) ? trim($_POST['username']) : "";
            $password = isset($_POST['password']) ? trim($_POST['password']) : "";
            $plat_name = isset($_POST['plat_name']) ? trim($_POST['plat_name']) : "";
            $is_lock = trim($_POST['is_lock']);
            if(!$id){ 
                $mssage['info']	  = '请选择平台账户所属';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            if(!$username){ 
                $mssage['info']	  = '用户名不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            $plat_logo = trim($_POST['plat_logo']);
            $contacter = trim($_POST['contacter']);
            $tell_mobile = trim($_POST['tell_mobile']);
            $tell_phone = trim($_POST['tell_phone']);
            $address = trim($_POST['address']);
            
            
            $plat_account = array();
            $plat_account['plat_name'] = $plat_name;
            $plat_account['username'] = $username;
            if($password){
                $plat_account['password'] = md5($password);
            }
            $plat_account['plat_logo'] = $plat_logo;
            $plat_account['contacter'] = $contacter;
            $plat_account['tell_mobile'] = $tell_mobile;
            $plat_account['tell_phone'] = $tell_phone;
            $plat_account['address'] = $address;
            $plat_account['is_lock'] = $is_lock;
            $plat_account['create_time'] = time();
            $result = $this->plat_account_model->update($plat_account,$id);
            if($result){
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = "platopen/index";
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = "platopen/index";
                echo json_encode($mssage);exit;
            }
        }
        $key = isset($_GET['key']) ? trim($_GET['key']) : "";
        if(!$key){redirect("platopen/index");}
        $plat = $this->plat_account_model->get_account_info(array('plat_en'=>$key));
        if(!$plat){
            redirect("platopen/index");
        }
        $this->template->assign('plat',$plat);
        $this->template->assign('segment_p','index');
        $this->template->load('platopen/account_edit');
    }
    
    //设置平台账户状态
    function setlock(){
        if($_GET){
            $mssage = array();
            $plat_id	   = trim($_GET['id']);
            $is_lock= trim($_GET['is_lock']);
            $data = array();
            $data['is_lock'] = $is_lock;
            $result = $this->plat_account_model->update($data,$plat_id);
            if($result){
                $mssage['info']	  = '更改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }	
    }
}
